package company.meituan_F;

import java.util.Scanner;

public class LianXi4 {

    private static int max = 0;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[][][] nums = new int[n][n][n];
        int[][][] used = new int[n][n][n];
        int pow = (int) Math.pow(n, 3);
        for (int i = 0; i < pow; i++) {
            int x = sc.nextInt();
            int y = sc.nextInt();
            int z = sc.nextInt();
            nums[x][y][z] = sc.nextInt();
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                for (int k = 0; k < n; k++) {
                    helper(n, i, j, k, nums, used, 0);
                }
            }
        }
        System.out.println(max);
    }

    private static void helper(int n, int x, int y, int z, int[][][] nums, int[][][] used, int sum) {
        used[x][y][z] = 1;
        int current = nums[x][y][z];
        boolean isMoved = false;
        if (canGo(n, x + 1, y, z, current, nums, used)) {
            helper(n, x + 1, y, z, nums, used, sum + current);
            isMoved = true;
        }
        if (canGo(n, x - 1, y, z, current, nums, used)) {
            helper(n, x - 1, y, z, nums, used, sum + current);
            isMoved = true;
        }
        if (canGo(n, x, y + 1, z, current, nums, used)) {
            helper(n, x, y + 1, z, nums, used, sum + current);
            isMoved = true;
        }
        if (canGo(n, x, y - 1, z, current, nums, used)) {
            helper(n, x, y - 1, z, nums, used, sum + current);
            isMoved = true;
        }
        if (canGo(n, x, y, z + 1, current, nums, used)) {
            helper(n, x, y, z + 1, nums, used, sum + current);
            isMoved = true;
        }
        if (canGo(n, x, y, z - 1, current, nums, used)) {
            helper(n, x, y, z - 1, nums, used, sum + current);
            isMoved = true;
        }
        used[x][y][z] = 0;
        if (!isMoved) {
            max = Math.max(sum, max);
        }
    }

    private static boolean canGo(int n, int x, int y, int z, int current, int[][][] nums, int[][][] used) {
        if (x < 0 || x >= n || y < 0 || y >= n || z < 0 || z >= n)
            return false;
        if (current < nums[x][y][z])
            return false;
        if (used[x][y][z] == 1)
            return false;
        return true;
    }
}
